var myapp = angular.module("app",['ionic','angular-md5']);

myapp.controller('Home',function($scope,$ionicLoading,$location,$interval,$timeout,md5,$http){
    //$ionicLoading.show();
 
    var GetQueryString = function(name){
          var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if(r!=null)return  unescape(r[2]); return null;
    };
    var channel = GetQueryString('channel');    

    $scope.rg = {
        phone:'',
        code:'',
        password:'',
        password2:'',
        channel:channel,
        province:"省份",
        city:"城市",
        zhifubao:'',
        name:''
    };
    console.log($scope.rg);
    $scope.sendcode = function(){

    };
    
	var HUD = function(template){

        $ionicLoading.show({
            template:template
        });

        $timeout(function(){
            $ionicLoading.hide();
        },1500);
    };

	$scope.counter = 60;
	$scope.countLabel = '发送';
	$scope.countdown = function(){
		$scope.stopped = $timeout(function(){
			$scope.counter --;
			$scope.countLabel = $scope.counter + 's';
			//console.log($scope.countLabel);
			if($scope.counter > 0){
				$scope.countdown();				
			}else{
				$scope.counter = 60;
				$scope.countLabel = '发送';
				$scope.stop();
			}
		},1000);
	};

    var zhuce_n = "img/button_yizhuce_n.png";
    var zhuce_s = "img/button_yizhuce_s.png";
    var weizhuce_n = "img/button_weizhuce_n.png";
    var weizhuce_s = "img/button_weizhuce_s.png";

    $scope.zhuce = zhuce_n;
    $scope.weizhuce = weizhuce_s;

    $scope.type = 1;

    $scope.select = function(index){
        $scope.type = index;
        if (index == 1){
            $scope.zhuce = zhuce_n;
            $scope.weizhuce = weizhuce_s;            
        }else{
            $scope.zhuce = zhuce_s;
            $scope.weizhuce = weizhuce_n;
        }


    };

    $scope.refreshPicCode = function(){

        var req = {
            method:'GET',
            url:PIC_CODE,
            headers:{
                'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
                'datasource':'6',
                'apiv':'2',
            },
            params:{}
            
        };
        $http(req).success(function(response){
            console.log(response)

            if(response.code == 1000){
                $scope.picimg = response.data.url;
                $scope.token = response.data.token;
            }else{
                HUD(response.message);
            }
        })       
    };
    
    $scope.refreshPicCode();    


	$scope.stop = function(){
		$timeout.cancel($scope.stopped);
		$scope.counter = 60;
	};

	$scope.sendAuthCode = function(){
		if($scope.rg.phone.length != 11){
            HUD('请填写正确的手机号');
            return;
        }
        
		if($scope.counter == 60){
			var params = {
				mobile:$scope.rg.phone,
                imageCode:$scope.rg.piccode,
                token:$scope.token
			};
            var url;
            if($scope.type == 1){
                url = AUTH_CODE_New;
            }else{
                url = AUTH_CODE_Old;
            }
            var req = {
                method:'GET',
                url:url,
                params:params
                
            };
            $http(req).success(function(response){
                HUD('发送成功');
                   
                if(response.code == 1000){
                    HUD('验证码已发送');
                    $scope.countdown();
                }else{
                    $scope.refreshPicCode(); 
                    $scope.rg.piccode = "";
                    HUD(response.message);
                }
            })
            
					
		}


	};    


    $http.get('vender/address.json').then(function(filedata){
        console.log(filedata);
        $scope.fileData = filedata.data;
    });

    $scope.provinceChanged = function(){
    
        $scope.fileData.forEach(function(object){
            if(object.pro_id == $scope.rg.province){
                $scope.cityArray = object.cityList;
                return;
            }
        });
    };

    
    $scope.confirm = function(){
        
    


		if($scope.rg.phone.length == 0 || $scope.rg.phone.length !== 11){
			HUD('请填写正确的手机号');
		}else if($scope.rg.password.length < 6  && $scope.type == 1){
			HUD('请填写6-16位密码');
		}else if($scope.rg.name.length == 0){
            HUD('请填写姓名');
        }else if($scope.rg.zhifubao.length == 0){
            HUD('请填写支付宝提现账户');
        }else if($scope.rg.province == '省份' || $scope.rg.city == '城市'){
            HUD('请选择省份城市');
        }else{
            var params = {}
            var url;
            if($scope.type == 1){
                url = REGISTE_New;
                params = {
                    "mobile":$scope.rg.phone,
                    "userPassword":md5.createHash($scope.rg.password || ""),
                    "smsCode":$scope.rg.code,
                    "realName":$scope.rg.name,
                    "alipayAccount":$scope.rg.zhifubao,
                    "provinceId":$scope.rg.province,
                    "cityId":$scope.rg.city,
                    "inviterCode":$scope.rg.channel
                };               
            }else{
                url = REGISTE_Old;
                params = {
                    "mobile":$scope.rg.phone,
                    "smsCode":$scope.rg.code,
                    "realName":$scope.rg.name,
                    "alipayAccount":$scope.rg.zhifubao,
                    "provinceId":$scope.rg.province,
                    "cityId":$scope.rg.city,
                    "inviterCode":$scope.rg.channel
                };                 
            }
            console.log(params);
			$ionicLoading.show();
            
            var req = {
                method:'Post',
                url:url,
                data:params
            };
            $http(req).success(function(response){
                console.log(response);
                $ionicLoading.hide();
                if(response.code == 1000){
                    if($scope.type == 1){
                        window.location = "http://expend.shangxinpifa.com/finish1.html"
                    }else{
                        window.location = "http://expend.shangxinpifa.com/finish2.html"
                    }
                    
                }else{
                    HUD(response.message);
                }
            })

			
		}
    };
})

myapp.controller('Index',function($scope){
    var GetQueryString = function(name){
          var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if(r!=null)return  unescape(r[2]); return null;
    };
    $scope.channel = GetQueryString('channel');
    $scope.jump = function(){
        console.log($scope.channel);
        window.location.href = "http://expend.shangxinpifa.com/register.html?channel=" + $scope.channel;
    }
})